//
//  List2ViewController.m
//  225视听百科
//
//  Created by 张元阳(10) on 14-12-29.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "List2ViewController.h"
#import "ConnectModel.h"
#import "Custom2ImageAndLabel.h"
#import "UIImageView+WebCache.h"
#import "List3ViewController.h"

@interface List2ViewController ()

@end

@implementation List2ViewController
- (void)dealloc
{
    [_name release];
    [_list2Array release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.list2Array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    self.view.backgroundColor = [UIColor lightGrayColor];
    [super viewDidLoad];
//    [self createList];
    [self create];
}
- (void)createList
{
    self.navigationItem.title = self.name;
    // 全部
    NSString *stt = [NSString stringWithFormat:@"http://app.9nali.com/777/common_tag/%ld/all?page_id=1&device=iPhone&version=1.1.5", self.category_id];
    [ConnectModel connectModelWithUrl:stt parmaters:nil finished:^(NSData *getData) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:getData options:NSJSONReadingMutableLeaves error:nil];
        NSMutableArray *arr = [dic objectForKey:@"list"];
        [_list2Array addObjectsFromArray:arr];
        
    }];
}
- (void)create
{
    // 其他
    NSString *str = [self.name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *str1 = [NSString stringWithFormat:@"http://app.9nali.com/777/common_tag/%ld/%@?page_id=1&device=iPhone&version=1.1.5", self.category_id, str];
    [ConnectModel connectModelWithUrl:str1 parmaters:nil finished:^(NSData *getData) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:getData options:NSJSONReadingMutableLeaves error:nil];
        NSMutableArray *arr = [dic objectForKey:@"list"];
        [_list2Array addObjectsFromArray:arr];
        
        NSInteger line = [_list2Array count] / 2;
        if ([_list2Array count] % 2 > 0) {
            line++;
        }
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        scroll.contentSize = CGSizeMake(self.view.frame.size.width, line * 200 + 10);
        [self.view addSubview:scroll];
        for (int i = 0; i < [_list2Array count]; i++) {
            
            Custom2ImageAndLabel *button = [[Custom2ImageAndLabel alloc] initWithFrame:CGRectMake(7 + ((self.view.frame.size.width / 2) - 5) * (i % 2), 10 + 200 * (i / 2), self.view.frame.size.width / 2 - 10, 190)];
            NSMutableDictionary *dic1 = [_list2Array objectAtIndex:i];
            button.tag = [[dic1 objectForKey:@"id"] integerValue];
            NSString *str = [dic1 objectForKey:@"coverLarge"];
            button.label.text = [dic1 objectForKey:@"title"];
            [button.imageV setImageWithURL:[NSURL URLWithString:str]];
            [scroll addSubview:button];
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [button release];
        }
    }];
   
}
- (void)buttonAction:(Custom2ImageAndLabel *)sender
{
    List3ViewController *list = [[List3ViewController alloc] init];
    list.Id = sender.tag;
    [self.navigationController pushViewController:list animated:YES];
    [list release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
